﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Prop;

namespace Web.pages
{
    public partial class changepassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txPasswordOld.Text = String.Empty;
                txPasswordNew.Text = String.Empty;
                txConfirm.Text = String.Empty;
            }
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
                if (String.IsNullOrEmpty(txPasswordOld.Text.Trim()))
                {
                    String message = "alert('กรุณากรอกรหัสผ่าน');";
                    if (scriptManager != null && scriptManager.IsInAsyncPostBack)
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), message, true);
                    else
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), message, true);
                }
                else if (String.IsNullOrEmpty(txPasswordNew.Text.Trim()))
                {
                    String message = "alert('กรุณากรอกรหัสผ่าน');";
                    if (scriptManager != null && scriptManager.IsInAsyncPostBack)
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), message, true);
                    else
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), message, true);
                }
                else if (String.IsNullOrEmpty(txConfirm.Text.Trim()))
                {
                    String message = "alert('กรุณายืนยันรหัสผ่าน');";
                    if (scriptManager != null && scriptManager.IsInAsyncPostBack)
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), message, true);
                    else
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), message, true);
                }

                else if (!txPasswordNew.Text.Trim().Equals(txConfirm.Text.Trim()))
                {
                    String message = "alert('รหัสผ่านไม่ตรงกัน');";
                    if (scriptManager != null && scriptManager.IsInAsyncPostBack)
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), message, true);
                    else
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), message, true);
                }
                else
                {
                    var o = Dao.USERDao.Init().Select(
                    new USER()
                    {
                        UWD = ((USER)Session["UserInfo"]).UID
                        ,
                        PWD = Core.Cipher.EncrytData(txPasswordOld.Text.Trim())
                    }
                    );

                    if (o.Count > 0)
                    {
                        Boolean result = Dao.USERDao.Init().Update(new USER()
                            {
                                UID = ((USER) Session["UserInfo"]).UID
                                ,
                                UWD = o.First().UWD
                                ,
                                PWD = Core.Cipher.EncrytData(txConfirm.Text.Trim())
                            });
                    }
                    else
                    {
                        String message = "alert('รหัสผ่านเดิมไม่ถูกต้อง');";
                        if (scriptManager != null && scriptManager.IsInAsyncPostBack)
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), message, true);
                        else
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), message, true);
                    }
                   
                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = ((USER)Session["UserInfo"]).UID
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }
            upl.Update();
        }
    }
}